package com.lepu.app.fun.chat.adapter;

import java.util.List;

import util.DateUtil;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.AnimationDrawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.app.utils.LoginUtil;
import com.app.utils.Setting;
import com.core.lib.utils.main.LogUtilBase;
import com.eyzhs.app.R;
import com.lepu.app.fun.chat.bean.ChatMessage;
import com.lepu.app.fun.chat.bean.Media;
import com.lepu.app.fun.chat.controller.base.AbsBaseFragment;
import com.lepu.app.widget.ImageLoaderUtil;
import com.lepu.app.widget.ManagerClipborad;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

import common.db.Constants;

/**
 * Created by lepu on 2014/12/19.
 */
public class ChattingAdapter extends BaseAdapter {
    //    private static Logger logger = LoggerFactory.getLogger(ChattingAdapter.class);
    private int lstPositonCache = -1;
    private boolean isCurrentRunning = false;
    List<ChatMessage> list;
    Context context;
    ImageLoader imageLoader;
    DisplayImageOptions displayImgOptions;

    private AbsBaseFragment.FragmentCallback callback;
    private AnimationDrawable lstDrable; //旧动画
    private AnimationDrawable curDrable;//当前动画
    private ImageView imageViewCache;
    private String personImage;

    public ChattingAdapter(Context context, List<ChatMessage> list, ImageLoader imageLoader, AbsBaseFragment.FragmentCallback callback) {
        this.list = list;
        this.context = context;
        this.imageLoader = imageLoader;
        this.callback = callback;
        displayImgOptions = new DisplayImageOptions.Builder().
                considerExifParams(true).
                cacheInMemory(true).
                cacheOnDisk(true).
                imageScaleType(ImageScaleType.IN_SAMPLE_INT)
                .bitmapConfig(Bitmap.Config.ALPHA_8).
                        showImageOnLoading(R.drawable.chat_bg_default).

                        build();
        lstDrable = new AnimationDrawable();
        curDrable = new AnimationDrawable();
        getImageHeader();
    }

    private void getImageHeader() {
        personImage = LoginUtil.getUserAvare();
//        if (avatarUrl != null && !TextUtils.isEmpty(avatarUrl)) {
//            personImage = MyApplication.getInstance().getLoginInfo().getAvatar();
//        }
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @SuppressLint("NewApi")
    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder holder = null;
        String url = "";
//      final boolean isFromPatient = AppContext.getAppContext().getApplicationType() == Constants.AppTypes.PATIENT;
        final boolean isFromPatient = true;

        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.chat_item_chat_list, null);
            holder = new ViewHolder();
            holder.laout_sender_chatting_content = (RelativeLayout) view.findViewById(R.id.laout_sender_chatting_content);
            holder.layout_receive_chatting_content = (RelativeLayout) view.findViewById(R.id.layout_receive_chatting_content);
            holder.sendVoiceTime = (TextView) view.findViewById(R.id.send_voice_time);
            holder.receiveVoiceTime = (TextView) view.findViewById(R.id.receive_voice_time);

            holder.senderHeader = (ImageView) view.findViewById(R.id.imgView_sender_header);
            holder.receiveHeader = (ImageView) view.findViewById(R.id.imgView_receive_header);
            holder.senderTxt = (TextView) view.findViewById(R.id.txt_sender_chatting_content);
            holder.receiveTxt = (TextView) view.findViewById(R.id.txt_receive_chatting_content);
            holder.receivePhoto = (ImageView) view.findViewById(R.id.imgView_receive_photo);
            holder.senderPhoto = (ImageView) view.findViewById(R.id.imgView_sender_photo);
            holder.receiveMediaProgress = (ProgressBar) view.findViewById(R.id.pb_receive_media_progress);
            holder.senderMediaProgress = (ProgressBar) view.findViewById(R.id.pb_sender_media_progress);
            holder.receiveMediaResetImg = (ImageView) view.findViewById(R.id.receive_media_reset);
            holder.senderMediaResetImg = (ImageView) view.findViewById(R.id.sender_media_reset);
            holder.senderTextResendImg = (ImageView) view.findViewById(R.id.imgView_sender_text_resend);
            holder.senderTextProgress = (ProgressBar) view.findViewById(R.id.pb_sender_text_resend);
            holder.sendVoice = (ImageView) view.findViewById(R.id.imgView_sender_voice);
            //todo
            holder.receiveVoice = (ImageView) view.findViewById(R.id.imgView_receive_voice);
            holder.time = (TextView) view.findViewById(R.id.chat_time);
            //// TODO: 2015/12/8  改版添加 
            holder.chat_deadling_time = (TextView) view.findViewById(R.id.chat_deadling_time);
            holder.chat_doctor_tips = (TextView) view.findViewById(R.id.chat_doctor_tips);

            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        //滑动时候
        curDrable.stop();
        lstDrable.stop();
        lstPositonCache = -1;
        isCurrentRunning = false;
        holder.sendVoice.setBackgroundResource(R.drawable.chat_icon_speech3_send);
        holder.sendVoice.setImageResource(0);
        holder.receiveVoice.setBackgroundResource(R.drawable.chat_kb_audio_receive_3);
        holder.receiveVoice.setImageResource(0);
        imageViewCache = null;
        //////////////////////////////
        setVisibleGone(holder);
        if (isFromPatient) {
            //holder.laout_sender_chatting_content.setBackgroundResource(R.drawable.chat_bg_patient_send);
            holder.laout_sender_chatting_content.setBackgroundResource(R.drawable.chat_right_bg_selector);
        } else {
            holder.laout_sender_chatting_content.setBackgroundResource(R.drawable.chat_bg_doctor);
        }
        final ViewHolder finalHolder = holder;
        holder.laout_sender_chatting_content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View p) {
                LogUtilBase.LogD(null, "私聊右侧点击");
                setVoiceClick(i, finalHolder);
            }
        });
        holder.sendVoice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View p) {
                LogUtilBase.LogD(null, "私聊右侧点击");
                setVoiceClick(i, finalHolder);
            }
        });

        holder.layout_receive_chatting_content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LogUtilBase.LogD(null, "私聊左侧点击");
                setLeftVoiceClick(i, finalHolder);
            }
        });

        holder.receiveVoice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LogUtilBase.LogD(null, "私聊左侧点击");
                setLeftVoiceClick(i, finalHolder);
            }
        });

        final ChatMessage chatMessage = list.get(i);

        holder.senderHeader.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                callback.gotoProfile(isFromPatient ? Constants.PATIENT : Constants.DOCTOR, String.valueOf(list.get(i).getFromUserId()));
            }
        });

        holder.receiveHeader.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ChatMessage chatMessage1 = list.get(i);
                callback.gotoProfile(!isFromPatient ? Constants.PATIENT : Constants.DOCTOR, String.valueOf(chatMessage1.getFromUserId()));
            }
        });


        holder.receiveVoiceTime.setText("");
        holder.sendVoiceTime.setText("");
        holder.senderTxt.setText("");
        holder.receiveTxt.setText("");
        holder.senderTxt.setVisibility(View.GONE);
        holder.receiveTxt.setVisibility(View.GONE);
        holder.sendVoice.setVisibility(View.GONE);
        holder.receiveVoice.setVisibility(View.GONE);
        holder.senderTxt.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                TextView textView = (TextView) view;
                ManagerClipborad.getInstance().getClipboardM(context).setText(textView.getText());
                Toast.makeText(context, R.string.copysuccess, Toast.LENGTH_SHORT).show();

                return false;
            }
        });
        holder.receiveTxt.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                TextView textView = (TextView) view;
                ManagerClipborad.getInstance().getClipboardM(context).setText(textView.getText());
                Toast.makeText(context, R.string.copysuccess, Toast.LENGTH_SHORT).show();
                return false;
            }
        });
        Media media = list.get(i).getMedia();
        if (list.get(i).getDirection() == Constants.Chat.CHAT_DIRECTION_RECEIVE) { //接收到的
            holder.receiveHeader.setVisibility(View.VISIBLE);
            holder.layout_receive_chatting_content.setVisibility(View.VISIBLE);
//            ImageLoader.getInstance().displayImage(Setting.getXmppImageUrl(String.valueOf(list.get(i).getFromUserId())),
//                    holder.receiveHeader, ImageLoaderUtil.circleoptions);
            if (list != null && !TextUtils.isEmpty(list.get(i).getGuestProfilePicId())) {
                url = list.get(i).getGuestProfilePicId();
                ImageLoader.getInstance().displayImage(url, holder.receiveHeader, ImageLoaderUtil.circleoptions);
                LogUtilBase.LogD("position :" + i, list.get(i).getGuestProfilePicId());
            } else {
                LogUtilBase.LogD("position__ :" + i, list.get(i).getGuestProfilePicId());
            }
            int messageType = list.get(i).getMessageType();
            switch (messageType) {
                case 1://text
                    holder.receiveTxt.setVisibility(View.VISIBLE);
                    holder.receiveTxt.setText(list.get(i).getContent());
                    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.receiveTxt.getLayoutParams();
                    params.width = RelativeLayout.LayoutParams.WRAP_CONTENT;
                    holder.receiveTxt.setLayoutParams(params);
//                    if (isDoctorType()) {
//                        addTextNoteListener(list.get(i).getSendStatus(), holder.receiveTxt, i);
//                    }
                    break;
                case 2://image
                    holder.receivePhoto.setVisibility(View.VISIBLE);
                    if (media != null && media.getFullName() != null && !TextUtils.isEmpty(media.getFullName())) {
                        url = "file://" + media.getFullName();
                        ImageLoaderUtil.getInstance().loadImage(imageLoader, url, holder.receivePhoto, displayImgOptions);
                    } else {
                        //下载失败使用
                        ImageLoaderUtil.getInstance().loadImage(imageLoader, media.getThumbnailImageId(), holder.receivePhoto, displayImgOptions);
                    }
//                    if (url != null) {
//                        if(url.equals("file://"))
//                        ImageLoaderUtil.getInstance().loadImage(imageLoader, url, holder.receivePhoto, displayImgOptions);
//                        LogUtilBase.LogD("downloaded_本地图片_" + i, " url: " + url);
//                    }
                    //holder.receivePhoto.setVisibility(View.VISIBLE);
                    holder.receivePhoto.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            callback.showLargePhoto(i);
                        }
                    });
//                    if (isDoctorType()) {
//                        addPhotoNoteListener(holder.receivePhoto, i);
//                    }
                    break;
                default:
                    break;
            }
            showBottomTime(i, view, holder, chatMessage);

        } else {//发送出来的
            //显示时间
            showTime(i, view, holder, chatMessage);
            holder.laout_sender_chatting_content.setVisibility(View.VISIBLE);
            holder.senderTxt.setVisibility(View.GONE);
            holder.receiveTxt.setVisibility(View.GONE);
            holder.sendVoice.setVisibility(View.GONE);
            holder.receiveVoice.setVisibility(View.GONE);
            holder.senderHeader.setVisibility(View.VISIBLE);
//            if (!list.get(i).getHostProfilePicId().isEmpty()) {
//                //url = IConstants.addressV3+"/"+list.get(i).getHostProfilePicId();
//                url = list.get(i).getHostProfilePicId();
//                ImageLoader.getInstance().displayImage(url, holder.senderHeader, getOptions());
//            }
            if (personImage != null && !personImage.isEmpty()) {
                ImageLoader.getInstance().displayImage(Setting.URL_API_HOST_HTTP_IMG + personImage, holder.senderHeader, getOptions());
            }
            int messageID = list.get(i).getMessageType();

            switch (messageID) {
                case 1://text
                    holder.senderTxt.setVisibility(View.VISIBLE);
                    holder.senderTxt.setText(list.get(i).getContent());
                    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.senderTxt.getLayoutParams();
                    params.width = RelativeLayout.LayoutParams.WRAP_CONTENT;
                    holder.senderTxt.setLayoutParams(params);
                    //holder.senderTxt.setWidth(200);
//                    if (isDoctorType()) {
//                        addTextNoteListener(list.get(i).getSendStatus(), holder.senderTxt, i);
//                    }
                    addReSendContentListener(list.get(i).getSendStatus(), holder.senderTextResendImg, i);
                    showTextProgress(list.get(i).getSendStatus(), holder.senderTextProgress);
                    holder.senderTextProgress.setTag(chatMessage.getMessageId());
                    break;
                case 2://image
                    holder.senderPhoto.setVisibility(View.VISIBLE);
                    if (!media.getFullName().isEmpty()) {
                        //Log.d("send Photo position=" + i + " url:", media.getFullName());
                        ImageLoaderUtil.getInstance().loadImage(imageLoader, "file://" + media.getFullName(), holder.senderPhoto, displayImgOptions);
                    }

                    holder.senderPhoto.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            callback.showLargePhoto(i);
                        }
                    });
                    if (list.get(i).getSendStatus() == Constants.Chat.CHAT_SEND_STATUS_SENT) {

                        holder.senderMediaProgress.setVisibility(View.VISIBLE);
                    } else if (list.get(i).getSendStatus() == Constants.Chat.CHAT_SEND_STATUS_LOST ||
                            list.get(i).getSendStatus() == Constants.Chat.CHAT_SEND_STATUS_UPLOAD_FAILED) {
                        holder.senderMediaProgress.setVisibility(View.GONE);
                        holder.senderMediaResetImg.setVisibility(View.VISIBLE);
                        //holder.senderPhoto.setImageResource(R.drawable.chat_send_photo_fail);
                        holder.senderMediaResetImg.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                callback.requestResendPhoto(i);
                            }
                        });
                    } else {
                        holder.senderPhoto.setVisibility(View.VISIBLE);
                        holder.senderMediaProgress.setVisibility(View.GONE);
                        holder.senderMediaResetImg.setVisibility(View.GONE);

//                        if (isDoctorType()) {
//                            addPhotoNoteListener(holder.senderPhoto, i);
//                        }
                    }
                    holder.senderMediaProgress.setTag(chatMessage.getMessageId());
                    holder.senderPhoto.setTag(chatMessage.getMessageId() + "image");
                    break;
                default:
                    break;
            }
        }
        // 关闭
        if( list!=null && list.size()>0 && list.size()-1==i && list.get(0).getIsClosed().equals("1")){
            holder.chat_deadling_time.setText("问题已关闭");
            holder.chat_deadling_time.setVisibility(View.VISIBLE);
            callback.hiddenChatBox(true);
        }
        return view;
    }
    /**
     * 显示标题时间 tips 提示 过期时间
     *
     * @param i
     * @param view
     * @param holder
     * @param chatMessage
     */
    private void showTime(int i, View view, ViewHolder holder, ChatMessage chatMessage) {
        if (i == 0) {
            if (chatMessage.getDirection() == 1 && chatMessage.getIsClosed().equals("0")) {
                holder.chat_doctor_tips.setVisibility(View.VISIBLE);
            } else { //显示聊天时间
                showTime(holder, chatMessage);
            }
        }
        if (i > 0) {
            holder.chat_doctor_tips.setVisibility(View.GONE);

            if (DateUtil.isCloseEnough(chatMessage.getSendTime(), list.get(i - 1).getSendTime())) {
                holder.time.setVisibility(View.GONE);
            } else {
                holder.time.setText(DateUtil.formatChatDateTime(chatMessage.getSendTime()));
                holder.time.setVisibility(View.VISIBLE);
            }
        }
        //时间固定显示到最后
        showBottomTime(i, view, holder, chatMessage);
    }

    private void showBottomTime(int i, View view, ViewHolder holder, ChatMessage chatMessage) {
        if (list.size() - 1 == i && chatMessage.getIsClosed().equals("0")) {
            //最后一个
            callback.hiddenChatBox(false);
            String time = formartTime(list.get(0).getRecordCreateTime());
            if (time.equals("##")) { //负数
                holder.chat_deadling_time.setText("问题已关闭");
                holder.chat_deadling_time.setVisibility(View.VISIBLE);
                callback.hiddenChatBox(true);
            } else if (time.contains("-")) {
                //format time
                showTime(holder, chatMessage);
            } else {
                //36 hour
                holder.chat_deadling_time.setText("该问题将于" + time + "小时后关闭");
                holder.senderTextProgress.setVisibility(View.GONE);
                holder.chat_deadling_time.setVisibility(View.VISIBLE);
                view.setMinimumHeight(context.getResources().getDimensionPixelSize(R.dimen.minheight_120));
            }
        }
    }

    /**
     * 显示时间
     *
     * @param holder
     * @param chatMessage
     */
    private void showTime(ViewHolder holder, ChatMessage chatMessage) {
        holder.time.setVisibility(View.VISIBLE);
        holder.time.setText(DateUtil.formatChatDateTime(chatMessage.getSendTime()));
    }

    /**
     * 格式化时间
     *
     * @param recordCreateTime
     * @return
     */
    private String formartTime(String recordCreateTime) {
        int time = 0;
        if (recordCreateTime == null || recordCreateTime.length() == 19) {
            return "36"; //默认36小时
        }
        if (recordCreateTime.substring(0, 1).equals("-")) {
            return "##"; //过期
        }
        String[] str = recordCreateTime.split(":");
        if (Integer.valueOf(str[1]) > 0) {
            time = Integer.valueOf(str[0]) + 1;
        } else if (Integer.valueOf(str[1]) == 0) {
            time = Integer.valueOf(str[0]);
        }
        return String.valueOf(time);
    }

    /**
     * 发送动画效果
     *
     * @param i
     * @param finalHolder
     */
    private void setVoiceClick(int i, final ViewHolder finalHolder) {
        final AnimationDrawable[] animationDrawable = {null};
        if (lstPositonCache == i) { //点击同一个
            if (isCurrentRunning) {
                callback.stopVoid(-1);
                curDrable.stop();
                isCurrentRunning = false;
                finalHolder.sendVoice.setBackgroundResource(R.drawable.chat_icon_speech3_send);
                finalHolder.sendVoice.setImageResource(0);
            } else {
                finalHolder.sendVoice.setBackgroundResource(0);
                finalHolder.sendVoice.setImageResource(R.drawable.vido_animation);
                curDrable = (AnimationDrawable) finalHolder.sendVoice.getDrawable();
                curDrable.start();
                isCurrentRunning = true;
                callback.playVoice(i, new OnVoicePlayEnd() {
                    @Override
                    public void OnPlayEnd() {
                        finalHolder.sendVoice.setBackgroundResource(R.drawable.chat_icon_speech3_send);
                        finalHolder.sendVoice.setImageResource(0);
                        isCurrentRunning = false;
                    }
                });
            }

        } else { //不是同一个
            if (null != imageViewCache) {
                if (imageViewCache.getId() == finalHolder.sendVoice.getId()) { //同一侧
                    imageViewCache.setImageResource(R.drawable.chat_icon_speech3_send);
                    imageViewCache.setBackgroundResource(0);
                } else {
                    imageViewCache.setImageResource(R.drawable.chat_kb_audio_receive_3);
                    imageViewCache.setBackgroundResource(0);
                }
            }
            callback.stopVoid(-1);
            callback.playVoice(i, new OnVoicePlayEnd() {
                @Override
                public void OnPlayEnd() {
                    finalHolder.sendVoice.setBackgroundResource(R.drawable.chat_icon_speech3_send);
                    finalHolder.sendVoice.setImageResource(0);
                    isCurrentRunning = false;

                }
            });
            curDrable.stop();
            finalHolder.sendVoice.setBackgroundResource(R.drawable.chat_icon_speech3_send);
            finalHolder.sendVoice.setImageResource(0);
            //上面是旧的 下面是新的对象
            finalHolder.sendVoice.setBackgroundResource(0);
            finalHolder.sendVoice.setImageResource(R.drawable.vido_animation);
            curDrable = (AnimationDrawable) finalHolder.sendVoice.getDrawable();
            curDrable.start();
            isCurrentRunning = true;
        }
        lstPositonCache = i;
        imageViewCache = finalHolder.sendVoice;
    }

    /**
     * 接收动画效果
     *
     * @param i
     * @param finalHolder
     */
    private void setLeftVoiceClick(int i, final ViewHolder finalHolder) {
        final AnimationDrawable[] animationDrawable = {null};
        if (lstPositonCache == i) { //点击同一个
            if (isCurrentRunning) {
                callback.stopVoid(-1);
                curDrable.stop();
                isCurrentRunning = false;
                finalHolder.receiveVoice.setBackgroundResource(R.drawable.chat_kb_audio_receive_3);
                finalHolder.receiveVoice.setImageResource(0);
            } else {
                finalHolder.receiveVoice.setBackgroundResource(0);
                finalHolder.receiveVoice.setImageResource(R.drawable.vido_left_animation);
                curDrable = (AnimationDrawable) finalHolder.receiveVoice.getDrawable();
                curDrable.start();
                isCurrentRunning = true;
                callback.playVoice(i, new OnVoicePlayEnd() {
                    @Override
                    public void OnPlayEnd() {
                        finalHolder.receiveVoice.setBackgroundResource(R.drawable.chat_kb_audio_receive_3);
                        finalHolder.receiveVoice.setImageResource(0);
                        isCurrentRunning = false;
                    }
                });
            }
        } else { //不是同一个
            setDefaultVoid(finalHolder);
            callback.stopVoid(-1);
            callback.playVoice(i, new OnVoicePlayEnd() {
                @Override
                public void OnPlayEnd() {
                    finalHolder.receiveVoice.setBackgroundResource(R.drawable.chat_kb_audio_receive_3);
                    finalHolder.receiveVoice.setImageResource(0);
                    isCurrentRunning = false;
                }
            });
            curDrable.stop();
            finalHolder.receiveVoice.setBackgroundResource(R.drawable.chat_kb_audio_receive_3);
            finalHolder.receiveVoice.setImageResource(0);
            //上面是旧的 下面是新的对象
            finalHolder.receiveVoice.setBackgroundResource(0);
            finalHolder.receiveVoice.setImageResource(R.drawable.vido_left_animation);
            curDrable = (AnimationDrawable) finalHolder.receiveVoice.getDrawable();
            curDrable.start();
            isCurrentRunning = true;
        }
        lstPositonCache = i;
        imageViewCache = finalHolder.receiveVoice;
    }

    /**
     * 设置默认
     *
     * @param finalHolder
     */
    private void setDefaultVoid(ViewHolder finalHolder) {
        if (null != imageViewCache) {
            if (imageViewCache.getId() == finalHolder.receiveVoice.getId()) { //同一侧
                imageViewCache.setImageResource(R.drawable.chat_kb_audio_receive_3);
                imageViewCache.setBackgroundResource(0);
            } else {
                imageViewCache.setImageResource(R.drawable.chat_icon_speech3_send);
                imageViewCache.setBackgroundResource(0);
            }
        }
    }

    private void handleVoiceTextWidth(RelativeLayout.LayoutParams params, String time) {
        int duration = Integer.valueOf(time.split("''")[0]);

        if ((time.split("''")[0]).contains("'")) {
            params.width = 350;
            return;
        }
        if (duration < 3) {
            params.width = 130;
        } else if (duration == 3) {
            params.width = 140;
        } else if (duration == 4) {
            params.width = 150;
        } else if (duration == 5) {
            params.width = 160;
        } else if (duration == 6) {
            params.width = 170;
        } else if (duration == 7) {
            params.width = 180;
        } else if (duration == 8) {
            params.width = 190;
        } else if (duration == 9) {
            params.width = 200;
        } else if (duration > 9 && duration < 20) {
            params.width = 230;
        } else if (duration > 19 && duration < 30) {
            params.width = 250;
        } else if (duration > 29 && duration < 40) {
            params.width = 270;
        } else if (duration > 39 && duration < 50) {
            params.width = 290;
        } else if (duration > 49 && duration < 60) {
            params.width = 310;
        } else if (duration == 60000) {
            params.width = 350;
        }
    }

    static class ViewHolder {
        ImageView receiveHeader, senderHeader, receivePhoto, senderPhoto, receiveMediaResetImg,
                senderMediaResetImg, senderTextResendImg;
        ImageView sendVoice, receiveVoice;
        TextView receiveTxt, senderTxt, time;
        //// 2015/12/8  在线咨询 一次一条记录模式
        TextView chat_deadling_time, chat_doctor_tips;
        ProgressBar receiveMediaProgress, senderMediaProgress, senderTextProgress;
        TextView sendVoiceTime, receiveVoiceTime;
        RelativeLayout layout_receive_chatting_content, laout_sender_chatting_content;
    }

    void setVisibleGone(ViewHolder holder) {
        holder.layout_receive_chatting_content.setVisibility(View.GONE);
        holder.laout_sender_chatting_content.setVisibility(View.GONE);
        holder.receiveHeader.setVisibility(View.GONE);
        holder.senderHeader.setVisibility(View.GONE);
        holder.receivePhoto.setVisibility(View.GONE);
        holder.senderPhoto.setVisibility(View.GONE);
        holder.receiveMediaResetImg.setVisibility(View.GONE);
        holder.senderMediaResetImg.setVisibility(View.GONE);
        holder.senderTextResendImg.setVisibility(View.GONE);
        holder.receiveTxt.setVisibility(View.GONE);
        holder.senderTxt.setVisibility(View.GONE);
        holder.receiveMediaProgress.setVisibility(View.GONE);
        holder.senderMediaProgress.setVisibility(View.GONE);
        holder.time.setVisibility(View.GONE);
        holder.chat_deadling_time.setVisibility(View.GONE);
        holder.senderTextProgress.setVisibility(View.GONE);
        holder.sendVoice.setVisibility(View.GONE);
        holder.receiveVoice.setVisibility(View.GONE);
        holder.chat_doctor_tips.setVisibility(View.GONE);
    }


    void addTextNoteListener(int status, View view, final int position) {
        switch (status) {
            case Constants.Chat.CHAT_SEND_STATUS_OFFLINE:
            case Constants.Chat.CHAT_SEND_STATUS_RECEIVED:
                view.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View view) {
                        callback.addTextToNote(view, position);
                        return true;
                    }
                });
                break;
        }
    }

    void addPhotoNoteListener(View view, final int position) {
        view.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                callback.addImageToNote(view, position);
                return true;
            }
        });
    }

    void addReSendContentListener(int status, View view, final int id) {
        switch (status) {
            case Constants.Chat.CHAT_SEND_STATUS_LOST:
                view.setVisibility(View.VISIBLE);
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        callback.reSendContent(id);
                    }
                });
                break;
            case Constants.Chat.CHAT_SEND_STATUS_OFFLINE:
                break;

        }
    }

    void showTextProgress(int status, ProgressBar progressBar) {
        if (status == Constants.Chat.CHAT_SEND_STATUS_SENT) {
            progressBar.setVisibility(View.VISIBLE);
        } else {
            progressBar.setVisibility(View.GONE);
        }
    }

    void showVoice(View view, final int id) {
//        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) view.getLayoutParams();
//        lp.width = 300;
//        view.setLayoutParams(lp);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                view.setBackground(AppContext.getAppContext().getDrawable(R.anim.voice_play_animation));
                callback.playVoice(id, new OnVoicePlayEnd() {
                    @Override
                    public void OnPlayEnd() {
                    }
                });
            }
        });
    }

//    class VoicePlayClickListener implements View.OnClickListener {
//        private AnimationDrawable voiceAnimation = null;
//
//        private ChatMessage message;
//
//
//        private boolean isPlaying = false;
//
//        ImageView voiceIconView;
//        private VoicePlayClickListener currentPlayListener = null;
//        ChatMessage currentMessage = null;
//
//        MediaPlayer mediaPlayer = null;
//
//        public VoicePlayClickListener(ChatMessage message, ImageView voiceIcon) {
//            voiceIconView = voiceIcon;
//        }
//
//        @Override
//        public void onClick(View view) {
//
//        }
//
//        public void stopPlayVoice() {
//            voiceAnimation.stop();
//            if (message.getDirection() == Constants.Chat.CHAT_DIRECTION_RECEIVE) {
//                voiceIconView.setImageResource(R.drawable.chat_voice_icon_receive_nor);
//            } else {
//                voiceIconView.setImageResource(R.drawable.chat_voice_icon_sender_nor);
//            }
//            // stop play voice
//            if (mediaPlayer != null) {
//                mediaPlayer.stop();
//                mediaPlayer.release();
//            }
//            isPlaying = false;
//        }
//        private void showAnimation() {
//            // play voice, and start animation
//            if (message.getDirection() == Constants.Chat.CHAT_DIRECTION_RECEIVE) {
//                voiceIconView.setImageResource(R.anim.voice_play_animation);
//            } else {
//                voiceIconView.setImageResource(R.anim.voice_play_animation);
//            }
//            voiceAnimation = (AnimationDrawable) voiceIconView.getDrawable();
//            voiceAnimation.start();
//        }
//
//
//    }

//    boolean isDoctorType() {
//        if (AppContext.getAppContext().getApplicationType() == Constants.AppTypes.DOCTOR) {
//            return true;
//        }
//        return false;
//    }

    public interface OnVoicePlayEnd {
        void OnPlayEnd();
    }

    public DisplayImageOptions getOptions() {
        DisplayImageOptions options = new DisplayImageOptions.Builder().
                considerExifParams(true).
                cacheInMemory(true).
                cacheOnDisk(true).
                imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2).
//                showImageOnLoading(AppContext.getAppContext().getApplicationType() == Constants.AppTypes.DOCTOR ? R.drawable.chat_icon_header_default : R.drawable.icon_header_default_patient).
        build();
        return options;
    }

}

